<template>
  <div class="content">
    <div class="inp">
      <div class="title">登陆系统</div>
      <el-form
        :model="account"
        :rules="rules"
        ref="ruleForm"
        label-width="0px"
        class="demo-ruleForm"
      >
        <el-form-item label="" prop="user">
          <el-input
            size="small"
            v-model="account.user"
            placeholder="请输入账号"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="pwd">
          <el-input
            :type="showPwd ? 'text' : 'password'"
            size="small"
            v-model="account.pwd"
            placeholder="请输入密码"
          >
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            <i
              slot="suffix"
              @click="changEyes"
              class="iconfont"
              :class="{ 'icon-yanjing1': showPwd, 'icon-biyanjing': !showPwd }"
            ></i>
          </el-input>
        </el-form-item>
      </el-form>

      <button @click="login">登陆</button>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { addRoute } from '@/router/index'
export default {
  data() {
    return {
      account: {
        user: "",
        pwd: "",
      },
      showPwd: "true",
      rules: {
        // required 必须有   message： 不满足要求的时候的报错信息   trigger： 校验时机（什么时候去校验）blur  change
        user: [
          { required: true, message: "请输入账号", trigger: "change" },
          { pattern:/^[a-zA-Z\d]+$/,message:"请输入正确格式账号",trigger: "change"}
        ],
        pwd: [
          { required: true, message: "请输入密码", trigger: "change" },
          { pattern:/^[a-zA-Z\d]+$/,message:"请输入正确格式密码",trigger: "change"}
        ]
      },
    };
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(async (res) => {
        if (res) {
          let man = {
            account: this.account.user,
            password: this.account.pwd,
          };
          let res = await axios.post(
            "http://1.15.179.44:3001/users/checkLogin",
            man
          );
          console.log(res);
          if (res.data.code === 0) {
            console.log(res);
            sessionStorage.setItem('token',res.data.token)
            sessionStorage.setItem('role',res.data.role)
            addRoute()
            this.$router.push("/home");
          } else {
            this.$message.error("账号或密码不正确");
          }
        }
      });
    },
    changEyes() {
      this.showPwd = !this.showPwd;
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  height: 100vh;
  width: 100vw;
  background: #2d3a4b;
  display: flex;
  // padding-top: 100px;
  .inp {
    width: 400px;
    height: 300px;
    // background: lightgreen;s
    margin: auto;

    .title {
      font-weight: 600;
      width: 100%;
      height: 30px;
      line-height: 30px;
      color: #fff;
      text-align: center;
    }
    .el-form {
      .el-form-item {
        margin-top: 20px;
      }
    }
    button {
      width: 100%;
      height: 35px;
      background: #409eff;
      border: 1px solid #409eff;
      border-radius: 4px;
      margin-top: 20px;
    }
  }
  /deep/ .el-input__inner{
    background: rgba(0,0,0,.2);
    color: #fff;
  }
}
</style>